﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.IO;
using System.Data;

namespace QidorSpider.Components.Util
{
    public class XmlHelper
    {
        protected XmlDocument xmlDoc;
        protected string xmlFile;

        public XmlHelper()
        {
            xmlDoc = new XmlDocument();
        }

        public XmlHelper(string _xmlFile)
        {
            xmlDoc = new XmlDocument();

            try
            {
                xmlDoc.Load(_xmlFile);

            }
            catch(System.Exception ex)
            {
                xmlDoc = null;
                throw ex;
            }
            xmlFile = _xmlFile;
        }

        ~XmlHelper()
        {
            xmlDoc = null;
        }

        public void NewXmlFile(string fileName, string strXml)
        {
            string filePath = Path.GetDirectoryName(fileName);

            if(!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(strXml);

            if(File.Exists(fileName))
            {
                File.SetAttributes(fileName, System.IO.FileAttributes.Normal);
            }

            xmlDoc.Save(fileName);

            xmlFile = fileName;
        }

        public string GetNodeValue(string nodePath)
        {
            XmlNode xmlNode = xmlDoc.SelectSingleNode(nodePath);

            if(xmlNode == null)
                return "";
            else
                return xmlNode.InnerText.ToString();
        }

        public DataView GetData(string xmlPathNode)
        {
            DataSet ds = new DataSet();
            StringReader reader = new StringReader(xmlDoc.SelectSingleNode(xmlPathNode).OuterXml);
            ds.ReadXml(reader);
            if(ds.Tables.Count == 0)
            {
                return null;
            }
            else
            {
                return ds.Tables[0].DefaultView;
            }
        }

        public DataView GetData(string nodeCollection, string node, string content)
        {
            XmlNodeList fatherNode = xmlDoc.GetElementsByTagName(nodeCollection);
            XmlNodeList nodes = fatherNode[0].ChildNodes;

            for(int i = 0; i < nodes.Count; i++)
            {
                for(int j = 0; j < nodes[i].ChildNodes.Count; j++)
                {
                    if(nodes[i].ChildNodes[j].Name == node && nodes[i].ChildNodes[j].InnerText == content)
                    {
                        StringReader reader = new StringReader(nodes[i].OuterXml);
                        DataSet ds = new DataSet();
                        ds.ReadXml(reader);
                        if(ds.Tables.Count == 0)
                        {
                            return null;
                        }
                        else
                        {
                            return ds.Tables[0].DefaultView;
                        }
                    }
                }
            }

            return null;
        }

        public void DeleteNode(string node)
        {
            XmlNodeList nodes = xmlDoc.GetElementsByTagName(node);
            XmlNode delNode = nodes[0];
            delNode.ParentNode.RemoveChild(delNode);
        }

        public void DeleteChildNodes(string nodeCollection, string node, string content)
        {
            XmlNodeList fatherNode = xmlDoc.GetElementsByTagName(nodeCollection);
            XmlNodeList nodes = fatherNode[0].ChildNodes;

            for(int i = 0; i < nodes.Count; i++)
            {
                for(int j = 0; j < nodes[i].ChildNodes.Count; j++)
                {
                    if(nodes[i].ChildNodes[j].Name == node && nodes[i].ChildNodes[j].InnerText == content)
                    {
                        fatherNode[0].RemoveChild(nodes[i]);
                        return;
                    }
                }
            }
        }

        public void InsertNode(string mainNode, string childNode, string element, string content)
        {
            XmlNode objRootNode = xmlDoc.SelectSingleNode(mainNode);
            XmlElement objChildNode = xmlDoc.CreateElement(childNode);
            objRootNode.AppendChild(objChildNode);
            XmlElement objElement = xmlDoc.CreateElement(element);
            objElement.InnerText = content;
            objChildNode.AppendChild(objElement);
        }

        public void EditNode(string element, string oldContent, string newContent)
        {
            XmlNodeList nodes = xmlDoc.GetElementsByTagName(element);

            for(int i = nodes.Count - 1; i >= 0; i--)
            {
                if(nodes[i].ChildNodes[0].InnerText == oldContent)
                {
                    nodes[i].ChildNodes[0].InnerText = newContent;
                }
            }
        }

        public void EditNodeName(string nodePath, string oldName, string newName)
        {
            XmlNode Nod = xmlDoc.SelectSingleNode(nodePath);
            string xml = Nod.InnerXml;

            DeleteNode(oldName);

            nodePath = nodePath.Substring(0, nodePath.LastIndexOf("/"));

            InsertElement(nodePath, newName, xml);
        }

        public void EditNodeValue(string nodePath, string newValue)
        {
            XmlNode Nod = xmlDoc.SelectSingleNode(nodePath);
            Nod.InnerText = newValue;
        }

        public void InsertElement(string mainNode, string element, string attrib, string attribContent, string content)
        {
            XmlNode objNode = xmlDoc.SelectSingleNode(mainNode);
            XmlElement objElement = xmlDoc.CreateElement(element);
            objElement.SetAttribute(attrib, attribContent);
            objElement.InnerText = content;
            objNode.AppendChild(objElement);
        }

        public void InsertElement(string mainNode, string element, string content)
        {
            XmlNode objNode = xmlDoc.SelectSingleNode(mainNode);
            XmlElement objElement = xmlDoc.CreateElement(element);
            objElement.InnerXml = content;
            objNode.AppendChild(objElement);
        }

        private readonly Object m_fileLock = new Object();
        public void Save()
        {
            try
            {
                if(File.Exists(xmlFile))
                {
                    File.SetAttributes(xmlFile, FileAttributes.Normal);
                }
                xmlDoc.Save(xmlFile);
            }
            catch(System.Exception ex)
            {
                throw ex;
            }
        }

        public void EditNodeValue(string nodeCollection, string node, string condition, string valueName, string value)
        {
            XmlNodeList fatherNode = xmlDoc.GetElementsByTagName(nodeCollection);
            XmlNodeList nodes = fatherNode[0].ChildNodes;

            for(int i = 0; i < nodes.Count; i++)
            {
                for(int j = 0; j < nodes[i].ChildNodes.Count; j++)
                {
                    if(nodes[i].ChildNodes[j].Name == node && nodes[i].ChildNodes[j].InnerText == condition)
                    {
                        XmlNode nod = nodes[i].SelectSingleNode(valueName);
                        nod.InnerText = value;
                        return;
                    }
                }
            }
        }

        ////ｴﾋｷｽｷｨﾗｨﾓﾃﾓﾚﾐﾞｸﾄtaskrun.xmlﾖﾐｲﾉｼｯｵﾄﾊﾖｵﾊｹﾓﾃ｣ｬﾆ萢鉎鯀ｻｿﾉﾊｹﾓﾃ
        ////ｴﾋｷｽｷｨｲｻﾍｨﾓﾃ｣ｬｵｱﾇｰｶﾔxmlｲﾙﾗ猖霈ﾆﾓﾐﾎﾊﾌ筌ｬｴﾋﾀ獎靨ｪﾖﾘﾐﾂﾉ霈ﾆ
        ////ﾍｬﾊｱﾐ靨ｪｸ・ﾂUrlcount｣ｬﾒｲﾐ昻ﾋﾊﾖｵｲｻﾊﾇｺﾜﾗｼﾈｷ｣ｬｵｫｵｱｲﾉｼｯﾈﾎﾎ・ﾂﾊﾇ｣ｬｻ眇ﾐﾋ｢ﾐﾂ
        //public void EditTaskrunValue(string TaskID, cGlobalParas.TaskState tState, string GUrlCount, string GTrueUrlCount, string GErrUrlCount, string GTrueErrUrlCount, string TrueUrlCount)
        //{
        //    XmlNodeList fathernode = xmlDoc.GetElementsByTagName("Tasks");
        //    XmlNodeList nodes = fathernode[0].ChildNodes;

        //    for(int i = 0; i < nodes.Count; i++)
        //    {
        //        for(int j = 0; j < nodes[i].ChildNodes.Count; j++)
        //        {
        //            //for (int m=0;
        //            if(nodes[i].ChildNodes[j].Name == "TaskID" && nodes[i].ChildNodes[j].InnerText == TaskID)
        //            {
        //                XmlNode nod = nodes[i].SelectSingleNode("TaskState");
        //                nod.InnerText = ((int)tState).ToString();
        //                nod = null;

        //                nod = nodes[i].SelectSingleNode("TrueUrlCount");
        //                nod.InnerText = TrueUrlCount;
        //                nod = null;

        //                nod = nodes[i].SelectSingleNode("GatheredUrlCount");
        //                nod.InnerText = GUrlCount;
        //                nod = null;

        //                nod = nodes[i].SelectSingleNode("GatheredTrueUrlCount");
        //                nod.InnerText = GTrueUrlCount;
        //                nod = null;

        //                nod = nodes[i].SelectSingleNode("ErrUrlCount");
        //                nod.InnerText = GErrUrlCount;
        //                nod = null;

        //                nod = nodes[i].SelectSingleNode("TrueErrUrlCount");
        //                nod.InnerText = GTrueErrUrlCount;
        //                nod = null;

        //                return;
        //            }
        //        }

        //    }
        //}




    }
}
